package cn.weiyang.controller;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

/**
 * 上传文件控制层
 * Created by Administrator on 2017/7/26.
 * */
@Controller
public class UpFileController {
    private Logger logger= LogManager.getLogger(UpFileController.class);
    @Value("${fileSaveDir}")
    private String fileSaveDir;
    @RequestMapping("/")
    public String show(){
        return "upfile";
    }
    @RequestMapping("/upfile")
    public String up(String username,MultipartFile[] upfile){
         for(MultipartFile file:upfile){
               logger.debug(username+" "+upfile);
             try {
                 /*系统随机生成文件名*/
                 String filename= UUID.randomUUID().toString();
                 /*将随机文件名和文件真实名字一起放在filemap中以便下载的时候方便对应*/
                 Const.fileMap.put(filename,file.getOriginalFilename());
                 /*将文件上传到指定路径*/
                 file.transferTo(new File(fileSaveDir+filename));
             } catch (IOException e) {
                 e.printStackTrace();
             }
         }
        return "success";
    }
}
